package com.zhou.jianzhi.service.impl;


import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.jianzhi.entity.base.PageResult;
import com.zhou.jianzhi.entity.po.ForumPost;
import com.zhou.jianzhi.entity.po.ForumReply;
import com.zhou.jianzhi.mapper.ForumPostMapper;
import com.zhou.jianzhi.mapper.ForumReplyMapper;
import com.zhou.jianzhi.service.ForumReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
* @author 黄嘉杰
* @description 针对表【forum_reply(帖子回复表)】的数据库操作Service实现
* @createDate 2025-01-23 11:15:21
*/
@Service
public class ForumReplyServiceImpl extends ServiceImpl<ForumReplyMapper, ForumReply>
implements ForumReplyService {
    @Autowired
    private ForumReplyMapper replyMapper;

    @Autowired
    private ForumPostMapper postMapper;

    @Override
    public PageResult<ForumReply> getReplyList(Long postId, Integer page, Integer size) {
        try {
            // 构建查询条件
            LambdaQueryWrapper<ForumReply> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ForumReply::getPostId, postId)
                    .eq(ForumReply::getStatus, 1)
                    .orderByAsc(ForumReply::getCreateTime);

            // 分页查询
            Page<ForumReply> pageParam = new Page<>(page, size);
            Page<ForumReply> replyPage = replyMapper.selectPage(pageParam, queryWrapper);

            return new PageResult<>(
                    replyPage.getRecords(),
                    replyPage.getTotal(),
                    replyPage.getCurrent(),
                    replyPage.getPages()
            );
        } catch (Exception e) {
            log.error("获取回复列表失败", e);
            throw new ServiceException("获取回复列表失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addReply(ForumReply reply) {
        try {
            // 检查帖子是否存在
            ForumPost post = postMapper.selectById(reply.getPostId());
            if (post == null || post.getStatus() == 0) {
                throw new ServiceException("帖子不存在");
            }

            // 设置初始值
            reply.setCreateTime(new Date());
            reply.setStatus(1);

            // 插入回复
            replyMapper.insert(reply);

            // 更新帖子回复数
            post.setReplyCount(post.getReplyCount() + 1);
            postMapper.updateById(post);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error("发表回复失败", e);
            throw new ServiceException("发表回复失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteReply(Long id) {
        try {
            // 获取回复信息
            ForumReply reply = replyMapper.selectById(id);
            if (reply == null || reply.getStatus() == 0) {
                throw new ServiceException("回复不存在");
            }

            // 软删除回复
            reply.setStatus(0);
            replyMapper.updateById(reply);

            // 更新帖子回复数
            ForumPost post = postMapper.selectById(reply.getPostId());
            if (post != null && post.getReplyCount() > 0) {
                post.setReplyCount(post.getReplyCount() - 1);
                postMapper.updateById(post);
            }
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error("删除回复失败", e);
            throw new ServiceException("删除回复失败");
        }
    }
}
